// components/index-home/quick-review/index.js
// 获取全局app实例
const app = getApp();
const storageManager = require('../../../manager/storageManager').getInstance();

Component({
  properties: {},
  data: {
    reviewCount: 0,
    cachedReviewCount: null,
    cacheExpireTime: null
  },
  lifetimes: {
    attached() {
      this.loadReviewCount();
    }
  },
  methods: {
    // 加载需要复习的单词数量
    loadReviewCount() {
      try {
        // 检查是否有缓存且未过期
        const now = Date.now();
        const cacheDuration = 60 * 1000; // 缓存1分钟
        
        if (this.data.cachedReviewCount !== null && 
            this.data.cacheExpireTime !== null && 
            now < this.data.cacheExpireTime) {
          // 使用缓存数据
          this.setData({ reviewCount: this.data.cachedReviewCount });
          return;
        }
        
        // 使用全局app实例的工具类
        const reviewScheduler = app.getReviewScheduler();
        const reviewCount = reviewScheduler.getReviewCount();
        
        // 更新缓存
        this.setData({
          reviewCount,
          cachedReviewCount: reviewCount,
          cacheExpireTime: now + cacheDuration
        });
        
        // 保存到本地存储，供其他组件使用
        storageManager.setStorage(storageManager.STORAGE_KEYS.LEARNING_DATA, { 
          reviewCount,
          lastUpdateTime: new Date().toISOString()
        });
      } catch (error) {
        console.error('加载复习数据失败:', error);
        this.setData({ reviewCount: 0 });
      }
    },
    
    // 点击进入记忆魔法屋
    onTapReview() {
      wx.navigateTo({ url: '/pages/vocabulary/review-magic/review-magic' })
    }
  }
})
